package com.hustbaidu.hustmap;

import com.hustbaidu.hustmap.HustMapView.OnPlaceLabelClickListener;
import com.hustbaidu.hustmap.HustMapView.OnPlaceLabelLongPressListener;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.ContextMenu;
import android.view.GestureDetector;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.View.OnCreateContextMenuListener;

public class PlaceLabelView extends View implements OnCreateContextMenuListener, OnMenuItemClickListener{
	private Bitmap mBitmap;
	private Bitmap dialogBitmap;
	private String placeName;
	private int width;
	private int height;
	private int offsetX;
	private int offsetY;
	private PlaceInfo place;
	private PlaceType type;
	
	private GestureDetector myGestureDetector;
	private OnPlaceLabelLongPressListener mPlaceLabelLongPressListener;
	private OnPlaceLabelClickListener mPlaceLabelClickListener;

	public PlaceLabelView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
	}

	public PlaceLabelView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	public PlaceLabelView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	
	public PlaceLabelView(Context context, String placeName) {
		super(context);
		// TODO Auto-generated constructor stub
		offsetX = 0;
		offsetY = 0;
		this.placeName = placeName;
		dialogBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.dialog);
		this.width = dialogBitmap.getWidth();
		this.height = dialogBitmap.getHeight();
//		System.out.println("PlaceLabelView : width " + width + " : " + "height " + height);
		mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
		
//		Paint paint = new Paint();
//		paint.setColor(Color.argb(204, 0, 0, 0));
//		paint.setStyle(Paint.Style.FILL);
//		paint.setTextSize(48);
//		paint.setAntiAlias(true);
//		int textWidth = (int)paint.measureText(placeName);
//		int areaWidth = mBitmap.getWidth() - 40;
//		
//		Canvas cv = new Canvas(mBitmap);
		this.setBackgroundResource(R.drawable.dialog_selector);
//		cv.drawBitmap(dialogBitmap, 0, 0, null);
//		cv.drawText(placeName, (mBitmap.getWidth() - textWidth) / 2, mBitmap.getHeight() / 2, paint);
		this.setVisibility(View.INVISIBLE);
		
		myGestureDetector = new GestureDetector(new MyGestureListener());
		setOnCreateContextMenuListener(this);
	}
	
	

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		// TODO Auto-generated method stub
//		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		setMeasuredDimension(width, height);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		// TODO Auto-generated method stub
		super.onDraw(canvas);
//		Rect src = new Rect(0, 0, width, height);
//		Rect dst = new Rect(0, 0, width, height);
		canvas.drawBitmap(mBitmap, 0, 0, null);
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		// TODO Auto-generated method stub
		super.onTouchEvent(event);
		myGestureDetector.onTouchEvent(event);
//		return super.onTouchEvent(event);
		return true;
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		// TODO Auto-generated method stub
		menu.setHeaderTitle("操作");
		menu.add(0, 0, 0, "详细介绍").setOnMenuItemClickListener(this);
		menu.add(0, 1, 0, "自定义信息").setOnMenuItemClickListener(this);
	}
	
	@Override
	public boolean onMenuItemClick(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case 0:
			Intent intent = new Intent();
			Bundle bundle = new Bundle();
//			bundle.putInt("place_id", place.getId());
			bundle.putParcelable("place", place);
			intent.putExtras(bundle);
			intent.setClass(getContext(), PlaceInfoActivity.class);
			getContext().startActivity(intent);
			break;

		default:
			break;
		}
		return false;
	}
	
	public void setOnPlaceLabelLongPressListener(OnPlaceLabelLongPressListener listener){
		this.mPlaceLabelLongPressListener = listener;
	}
	
	public void setOnPlaceLabelClickListener(OnPlaceLabelClickListener listener){
		this.mPlaceLabelClickListener = listener;
	}
	

	public void setPlaceName(String placeName) {
		this.placeName = placeName;
	}
	
	public void move(int distanceX, int distanceY)
	{
		offsetX = offsetX - distanceX;
		offsetY = offsetY - distanceY;
//		this.layout(offsetX, offsetY, offsetX + width, offsetY + height);
	}
	
	public void moveTo(int distanceX, int distanceY)
	{
		offsetX = distanceX;
		offsetY = distanceY;
//		this.layout(offsetX, offsetY, offsetX + width, offsetY + height);
	}
	
	public int getOffsetX() {
		return offsetX;
	}

	public int getOffsetY() {
		return offsetY;
	}

	public PlaceInfo getPlace() {
		return place;
	}

	public void setPlace(PlaceInfo place, PlaceType type) {
		this.place = place;
		this.type = type;
		Paint paint = new Paint();
		paint.setColor(Color.argb(204, 0, 0, 0));
		paint.setStyle(Paint.Style.FILL);
		paint.setTextSize(36);
		paint.setAntiAlias(true);
		int textWidth = (int)paint.measureText(place.getPlaceName());
//		int areaWidth = mBitmap.getWidth() - 40;
		
		mBitmap.eraseColor(Color.TRANSPARENT);
		Canvas cv = new Canvas(mBitmap);
//		cv.drawBitmap(dialogBitmap, 0, 0, null);
		System.out.println(mBitmap.getWidth() + " : " + textWidth + " : " + (mBitmap.getWidth() - textWidth) / 2);
		cv.drawText(place.getPlaceName(), (mBitmap.getWidth() - textWidth) / 2, mBitmap.getHeight() / 2, paint);
	}



	private class MyGestureListener extends SimpleOnGestureListener{
		
		@Override
		public boolean onDown(MotionEvent e) {
			// TODO Auto-generated method stub
			return super.onDown(e);
		}

		@Override
		public boolean onSingleTapConfirmed(MotionEvent e) {
			// TODO Auto-generated method stub
			
//			System.out.println("onSingleTapConfirmed");
//			System.out.println("1 -- " + e.getX() + " : " + e.getY());
			if(getWidth() - e.getX() < 50){				
				setVisibility(View.INVISIBLE);
			}
			else{
				if(mPlaceLabelClickListener != null){
					mPlaceLabelClickListener.onClick(place, type);
				}
			}
			return super.onSingleTapConfirmed(e);
//			return true;
		}

		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			// TODO Auto-generated method stub
//			System.out.println("onSingleTapUp");
			return super.onSingleTapUp(e);
		}

		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,
				float distanceX, float distanceY) {
			// TODO Auto-generated method stub
			return super.onScroll(e1, e2, distanceX, distanceY);
		}
		
		@Override
		public void onLongPress(MotionEvent e) {
			// TODO Auto-generated method stub
			showContextMenu();
			super.onLongPress(e);
		}
		
	}
	
}
